<?php

class Managecatalogue extends CI_Controller {

    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
            $this->load->model('news_m');
        } else {
            //die($id . ' ' . $adminname);
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('news_m');
        $this->load->model('catalogue_m');
    }

    function index() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            /*   $this->load->library('pagination');
              $offset = $this->uri->segment(3);
              $limit = 12;
              $config['base_url'] = site_url('admin/managecatalogue');
              $config['uri_segment'] = 3;
              $config['total_rows'] = $this->db->count_all('catalogue');
              $config['per_page'] = $limit;
              $this->pagination->initialize($config);
              $data['paginator'] = $this->pagination->create_links(); */

            $data['catalogue'] = $this->catalogue_m->get_catalogue();

            $this->load->view('admin/common/header');
            $this->load->view('admin/catalogue', $data);
            $this->load->view('admin/common/footer');
        } else {
            redirect(base_url() . 'admin/login');
        }
    }

    function add() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (isset($_POST['add']) && $_POST['add'] == 'Add') {

                $data['name'] = $this->input->post('title');
                $data['alias'] = $this->input->post('slug');
                $data['parent_id'] = $this->input->post('parent');
                $data['language'] = $this->input->post('language');
                $data['order'] = $this->input->post('order');
                $data['quantity'] = $this->input->post('quantity');
                $data['thumb'] = $this->_upload_files();
                $data['show'] = $this->input->post('show');
                if ($data['show'] == '')
                    $data['show'] = 0;
                if(!is_numeric(trim($data['order'])))
                    $data['order'] = 0;
                if(!is_numeric(trim($data['quantity'])))
                    $data['quantity'] = 4;
                //die(print_r($data));
                if (trim($data['name']) == '' || trim($data['alias']) == '')
                    redirect(base_url() . 'admin/managecatalogue/add');

                $id = $this->catalogue_m->add($data);
                if ($id > 0) {
                    //$this->_upload_files($id);
                    redirect(base_url() . 'admin/managecatalogue');
                }
                else
                    die('Lỗi thêm danh mục mới!');
            }else {

                $data['catalogue'] = $this->catalogue_m->get_catalogue();

                $this->load->view('admin/common/header');
                $this->load->view('admin/catalogue_add', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function update($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id) || $id < 0)
                redirect(base_url() . 'admin/managecatalogue');

            if (isset($_POST['update']) && $_POST['update'] == 'Update') {
                $data['name'] = $this->input->post('title');
                $data['alias'] = $this->input->post('slug');
                $data['parent_id'] = $this->input->post('parent');
                $data['order'] = $this->input->post('order');
                $data['quantity'] = $this->input->post('quantity');
                $data['language'] = $this->input->post('language');
                $thumb = $this->_upload_files();
                if($thumb != "")
                    $data['thumb'] = $thumb;
                $data['show'] = $this->input->post('show');
                if ($data['show'] == '')
                    $data['show'] = 0;
                if(!is_numeric(trim($data['order'])))
                    $data['order'] = 0;
                if(!is_numeric(trim($data['quantity'])))
                    $data['quantity'] = 4;
                //die(print_r($data));
                if (trim($data['name']) == '' || trim($data['alias']) == '')
                    redirect(base_url() . 'admin/managecatalogue/update/' . $id);

                if ($this->catalogue_m->update($id, $data)) {
                    //$this->_upload_files($id);
                    redirect(base_url() . 'admin/managecatalogue');
                }
                else
                    die('Lỗi cập nhật danh mục');
            }else {
                //die('xxx');
                $data['catalogue'] = $this->catalogue_m->get(0, 0, $id);
                $data['catalogues'] = $this->catalogue_m->get_catalogue();
                $this->load->view('admin/common/header');
                $this->load->view('admin/catalogue_edit', $data);
                $this->load->view('admin/common/footer');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete($id) {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!is_numeric($id))
                redirect(base_url() . 'admin/managecatalogue');
            if ($this->catalogue_m->delete($id)) {
                if (file_exists('uploads/catalogue/' . $id)) {
                    if (file_exists('uploads/catalogue/' . $id . '/thumb.jpg'))
                        unlink('uploads/catalogue/' . $id . '/thumb.jpg');
                    rmdir('uploads/catalogue/' . $id);
                }
                redirect(base_url() . 'admin/managecatalogue');
            }
            else
                die('Lỗi xóa danh mục');
        } else
            redirect(base_url() . 'admin/login');
    }

    function delete_catalogue() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            $ids = $this->input->post('selected');
            if (is_array($ids)) {
                if ($this->catalogue_m->delete($ids))
                    redirect(base_url() . 'admin/managecatalogue');
                else
                    die('Lỗi xóa danh mục');
            }else {
                redirect(base_url() . 'admin/managecatalogue');
            }
        } else
            redirect(base_url() . 'admin/login');
    }

    public function _upload_files() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!file_exists('uploads/catalogue/thumbs')) {
                mkdir('uploads/catalogue/thumbs', 0777);
                chmod('uploads/catalogue/thumbs', 0777);
            }
            
                
                $upload_cfg['max_size'] = '2048';
                $upload_cfg['upload_path'] = 'uploads/catalogue';
                $upload_cfg['allowed_types'] = 'gif|jpg|png';
                $upload_cfg['encrypt_name'] = FALSE;
                $upload_cfg['overwrite'] = FALSE;               
                $this->load->library('upload', $upload_cfg);
                
               if($this->upload->do_upload('cat_thumb')){ 
                        $img = $this->upload->data();
                        if($this->_thumb($img['full_path'])){    
                          return $img['file_name'];
                        }
                }
            
        } else
            redirect(base_url() . 'admin/login');
    }
    
    function _thumb($img){
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
			$config['image_library'] = 'gd2';
			$config['source_image'] = $img;
			$config['new_image'] = 'uploads/catalogue/thumbs';
			$config['cread_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['width'] = 600;
			$config['height'] = 360;
			$this->load->library('image_lib',$config);
			if(!$this->image_lib->resize())
				return false;
			else
				return true;
		
        }else{
            redirect(base_url() . 'admin/login');
        }
    }

}